//
//  secondViewViewController.m
//  mutiThread
//
//  Created by admin on 17/4/9.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "secondViewViewController.h"
#import "ViewController.h"
#import "KCImageData.h"
#import "secondViewViewController.h"

#define ROW_COUNT 6
#define COLUMN_COUNT 3
#define ROW_HEIGHT 100
#define ROW_WIDTH ROW_HEIGHT
#define CELL_SPACING 10
#define CELL_WIDTH ([UIScreen mainScreen].bounds.size.width-(ROW_COUNT-1)*CELL_SPACING)/ROW_COUNT
#define CELL_HEIGHT 200
@interface secondViewViewController (){
    NSMutableArray*imageViews;
    NSMutableArray *imageNames;
    NSMutableArray *threads;
}

@end

@implementation secondViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	CGRect rect = CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    UIView *view = [[UIView alloc]initWithFrame:rect];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    [self loadui];
    self.navigationItem.title=@"返回";
    // Do any additional setup after loading the view.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)loadui{
    UIButton *mybutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
           mybutton.frame=CGRectMake(10, 400, 100, 20);
    [mybutton setTitle:@"加载图片" forState:UIControlStateNormal];
    [mybutton setTitle:@"好" forState:UIControlStateSelected];
    [mybutton addTarget:self action:@selector(btnLoad) forControlEvents:UIControlEventTouchUpInside];
   // [buttonStart addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mybutton];
    
    UIButton *mybutton2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    mybutton2.frame=CGRectMake(110, 400, 100, 20);
    [mybutton2 setTitle:@"返回" forState:UIControlStateNormal];
    [mybutton2 setTitle:@"好" forState:UIControlStateSelected];
    [mybutton2 addTarget:self action:@selector(btnback) forControlEvents:UIControlEventTouchUpInside];
    // [buttonStart addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mybutton2];
    UIButton *mybutton3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    mybutton3.frame=CGRectMake(210, 400, 100, 20);
    [mybutton3 setTitle:@"暂停" forState:UIControlStateNormal];
    [mybutton3 setTitle:@"好" forState:UIControlStateSelected];
    [mybutton3 addTarget:self action:@selector(btnStop) forControlEvents:UIControlEventTouchUpInside];
    // [buttonStart addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mybutton3];
    
    
    imageViews=[[NSMutableArray alloc] init];
    imageNames=[[NSMutableArray alloc] init];
    for(int i=0;i<COLUMN_COUNT;i++){
        for (int j=0; j<ROW_COUNT; j++) {
            UIImageView *imagecell=[[UIImageView alloc]init];
            imagecell.frame=CGRectMake(i*(CELL_WIDTH+CELL_SPACING),
                                       j*(CELL_HEIGHT+CELL_SPACING),
                                       CELL_WIDTH,
                                       CELL_HEIGHT);
            imagecell.contentMode=UIViewContentModeScaleAspectFit;
            [self.view addSubview:imagecell];
            [imageViews addObject:imagecell];
            [imageNames addObject:[NSString stringWithFormat:@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%i.jpg",i*ROW_COUNT+j+1]];
        }
    }
}
-(void)btnLoad{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"确定按钮" delegate:self cancelButtonTitle:@"多线程加载" otherButtonTitles:@"主线程加载", nil];
    //[alert show];
    [self mutiThreadLoad];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickButtonAtIndex:%d",buttonIndex);
    if (buttonIndex==0) {
        [self mutiThreadLoad];
    }
}
-(void)mutiThreadLoad{
    int count=ROW_COUNT*COLUMN_COUNT;
    threads=[NSMutableArray arrayWithCapacity:count];
    for(int i=0;i<count;i++){
        
        NSThread *ithread=[[NSThread alloc]initWithTarget:self selector:@selector(requestData:) object:[NSNumber numberWithInt:i]];
        [threads addObject:ithread];
        
    }
    for (int i=0; i<count; i++) {
        NSThread *thread=threads[i];
        [thread  start];
    }
}
-(void)requestData:(NSNumber *)number{
    int i=[number integerValue];
    NSThread *currentthread =threads[i];
    if(currentthread.isCancelled){
        NSLog(@"线程(%@) 被取消了!",currentthread);
        [NSThread exit];
    }
    NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNames[i]]];
    KCImageData *imageObject=[[KCImageData alloc]init];
    imageObject.index=i;
    imageObject.data=imageData;
    [self performSelectorOnMainThread:@selector(showImage:) withObject:imageObject waitUntilDone:NO];
}
-(void)showImage:(KCImageData*)imagedata{
    UIImage *image=[UIImage imageWithData:imagedata.data];
    UIImageView *imageview= imageViews[imagedata.index];//.image=image;
    imageview.image=image;
}
-(void)btnStop{
    for (int i=0; i<ROW_COUNT*COLUMN_COUNT; i++) {
        NSThread *ithread =threads[i];
        if(!ithread.isFinished){
           [ithread cancel];
        }
    }
}
-(void)btnback{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)finishload{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"确定按钮" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
    [alert show];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
